package common.kit;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.alibaba.fastjson.JSONObject;
import com.jfinal.kit.PathKit;
import com.jfinal.weixin.sdk.utils.HttpUtils;
import com.jfinal.wxaapp.api.WxaAccessTokenApi;

public class ImageKits {

	public static String getQrcode(String currentPath , JSONObject map) {
//		String pathFile = "/download/qrcode/" + fileName + ".png" ;
		String accessToken = WxaAccessTokenApi.getAccessTokenStr() ;
		String url = "https://api.weixin.qq.com/wxa/getwxacodeunlimit?access_token=" + accessToken ;
		InputStream inputStream = HttpUtils.download(url, map.toJSONString() );
		File file = new File(PathKit.getWebRootPath() + "/download/qrcode");
		if (!file.exists()) {
			file.mkdirs();
		}
		int index;
		byte[] bytes = new byte[1024];
		FileOutputStream downloadFile;
		try {
			downloadFile = new FileOutputStream(PathKit.getWebRootPath() + currentPath );
			while ((index = inputStream.read(bytes)) != -1) {
				downloadFile.write(bytes, 0, index);
				downloadFile.flush();
			}
			downloadFile.close();
			inputStream.close();
		}catch (IOException e) {
			e.printStackTrace();
		}
		return currentPath ;
	}
}
